﻿using PARK.Common.Core.Manager;
using PARK.Common.Enum;
using PARK.Common.Extension;
using PARK.Common.Filter;
using PARK.Dependency;
using PARK.DynamicApiController;
using PARK.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;
using PARK.Common.Helper;
using PARK.JsonSerialization;
using PARK.CardAcct.Interfaces;
using PARK.CardAcct.Entitys.Dto.employee;
using PARK.System.Interfaces.Permission;
using PARK.Common.Util;
using PARK.System.Entitys.Permission;
using System.Text;
using PARK.CardAcct.Entitys;
using PARK.CardAcct.Entitys.Dto.Wechat;
using Microsoft.AspNetCore.Authorization;
using PARK.Welfare.Entitys;
using PARK.Welfare.Entitys.Dto.WelfareDetail;

namespace PARK.CardAcct.Welfare
{
    /// <summary>
    /// 小程序用户
    /// </summary>
    [ApiDescriptionSettings(Tag = "Wechat", Name = "WelfareUser", Order = 200)]
    [Route("api/wechat/[controller]")]
    public class WelfareUserService : IWechatUserService, IDynamicApiController, ITransient
    {
        private readonly ISqlSugarRepository<WelfareDetailEntity> _baseEmployeeRepository;
        private readonly SqlSugarScope _db;
        private readonly IUserManager _userManager;
        private readonly IOrganizeService _organizeService; // 机构表仓储

        /// <summary>
        /// 初始化一个<see cref="WelfareUserService"/>类型的新实例
        /// </summary>
        public WelfareUserService(
            ISqlSugarRepository<WelfareDetailEntity> baseEmployeeRepository,
            IUserManager userManager,
            IOrganizeService organizeService)
        {
            _baseEmployeeRepository = baseEmployeeRepository;
            _db = _baseEmployeeRepository.Context;
            _userManager = userManager;
            _organizeService = organizeService;
        }

        #region 获取用户福利发放记录
        /// <summary>
        /// 获取用户福利发放记录
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetWelDetInfo")]
        public async Task<dynamic> GetWelDetInfo([FromQuery] WelfareDetailListQueryInput input)
        {
            var sidx = input.sidx == null ? "billDate" : input.sidx;
            var userInfo = await _userManager.GetUserInfo();
            var _EmployeeId = "0";
            if (userInfo != null && userInfo.UserKind == 1 && !string.IsNullOrEmpty(userInfo.EmployeeId))
            {
                _EmployeeId = userInfo.EmployeeId;
            }

            var data = await _db.Queryable<WelfareDetailEntity>()
                .Where(p => p.EmployeeId.Equals(_EmployeeId) && p.IsAccounted == 1 && p.BussKind == 1)
                .Select(it => new WelfareDetailListOutput
                {
                    detailsid = it.Detailsid,
                    billId = it.BillId,
                    employeeId = it.EmployeeId,
                    jobNo = it.JobNo,
                    reallName = it.ReallName,
                    departId = it.DepartId,
                    departName = it.DepartName,
                    cardNo = it.CardNo,
                    amount = it.Amount,
                    summary = it.Summary,
                    isAccounted = it.IsAccounted,
                    accountedDate = Convert.ToDateTime(it.AccountedDate).ToString("yyyy-MM-dd"),
                    billDate = Convert.ToDateTime(it.BillDate).ToString("yyyy-MM-dd"),
                    begDate = Convert.ToDateTime(it.BegDate).ToString("yyyy-MM-dd"),
                    endDate = Convert.ToDateTime(it.EndDate).ToString("yyyy-MM-dd"),
                    conSumAmt = it.ConSumAmt,
                    balance = it.Balance,
                }).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
            return PageResult<WelfareDetailListOutput>.SqlSugarPageResult(data);
        }
        #endregion

    }
}
